package org.jeecgframework.core.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowPic extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {

	private static final String GIF = "image/gif;charset=GB2312";// 设定输出的类型

	private static final String JPG = "image/jpeg;charset=GB2312";
	
	private static final String PNG = "image/png;charset=GB2312";

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ShowPic() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try{
			String imagePath = request.getParameter("imgpath");// 输出图片的类型的标志	
			
			//System.out.println(specNum+"    "+imagePath);
			response.reset();
	
			OutputStream output = response.getOutputStream();// 得到输出流
			ServletContext context = getServletContext();// 得到背景对象
			InputStream imageIn = context.getResourceAsStream(imagePath);// 文件流
			byte [] data = new byte[1024];
			while(imageIn.read(data) != -1){ 
				output.write(data);
			}  
			
			response.setContentType("image/*"); //设置返回的文件类型 
			
			output.flush();
			output.close();
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException("获取图片失败");
		}
	}
}
